<?php

class Posts {

    private $db;
    private $cfg;

    public function __construct($db, $cfg) {

        $this->cfg = $cfg;

        $this->db = $db;
        
    }

    public function countPosts() {
        $query = 'SELECT count(*) FROM ' . $this->cfg->get('post_tbl');
        return $this->db->Count($query);
    }

    public function Paginator() {
        $posts = $this->countPosts();
        $pages = $posts / $this->cfg->get('posts_per_page');
        $pages = ceil($pages);

        for ($i = 1; $i <= $pages; $i++) {
            echo '<a href=' . $this->cfg->get('index') . '?page=' . $i . '>strona ' . $i . '</a> ';
        }
    }

    private function getPageNr() {
        if (isset($_GET['page'])) {
            $param = $_GET['page'];
        } else {
            $param = 0;
        }
        return $param;
    }

    public function getPosts() {
        $start = ($this->getPageNr() - 1) * $this->cfg->get('posts_per_page');
        $stop = $this->cfg->get('posts_per_page');
        $query = 'SELECT * FROM ' . $this->cfg->get('post_tbl') . ' ORDER BY timestamp DESC LIMIT ' . $start . ', ' . $stop;
        return $this->db->getData($query);
    }

    private function getUniqIds() { //pobieranie numerow id potrzebnych postow
        $start = ($this->getPageNr() - 1) * $this->cfg->get('posts_per_page');
        $stop = $this->cfg->get('posts_per_page');
        $query = 'SELECT uniqid FROM ' . $this->cfg->get('post_tbl') . ' ORDER BY timestamp DESC LIMIT :start, :stop';
        $arr = array(
            ':start' => $start,
            ':stop' => $stop
        );

        $posts_ids = $this->db->exec($query, $arr)->fetchAll(PDO::FETCH_COLUMN);
        $posts_ids_string = implode("', '", $posts_ids);

        $query2 = 'SELECT uniqid FROM ' . $this->cfg->get('reply_tbl') . ' WHERE reply_to IN (\'' . $posts_ids_string . '\')';
        $replies_ids = $this->db->query($query2)->fetchAll(PDO::FETCH_COLUMN);

        $result = implode("', '", array_merge($posts_ids, $replies_ids));

        return $result;
    }

    public function getReplies() {
        $posts = $this->getUniqIds();
        $query = 'SELECT * FROM ' . $this->cfg->get('reply_tbl') . ' WHERE reply_to IN (\'' . $posts . '\') ORDER BY postID ASC';
        //$query = 'SELECT * FROM ' . $this->cfg->get('reply_tbl');

        return $this->db->getData($query);
    }
    
    public function getImages(){
        $posts = $this->getUniqIds();
        $query = 'SELECT * FROM ' . $this->cfg->get('img_tbl') . ' WHERE to_post IN (\'' . $posts . '\')';
        return $this->db->getData($query);
    }

    public function addPost() {
        if (isset($_POST['txt']) && !isset($_GET['reply_to'])) {
            $qry = "INSERT INTO " . $this->cfg->get('post_tbl') . " (userID, timestamp, txt, uniqid) values (:userID, :timestamp, :txt, :uniqid)";

            $arr = array(
                ':userID' => $_SESSION['user']['id'],
                ':timestamp' => time(),
                ':txt' => $_POST['txt'],
                ':uniqid' => $_SESSION['uniqid']
                );
            $this->db->exec($qry, $arr);

            $this->redir();
        } elseif (isset($_POST['txt']) && isset($_GET['reply_to'])) {
            $qry = "INSERT INTO " . $this->cfg->get('reply_tbl') . " (userID, timestamp, txt, reply_to, uniqid) values (:userID, :timestamp, :txt, :reply_to, :uniqid)";

            $arr = array(
                ':userID' => $_SESSION['user']['id'],
                ':timestamp' => time(),
                ':txt' => $_POST['txt'],
                ':reply_to' => $_GET['reply_to'],
                ':uniqid' => $_SESSION['uniqid']
                );

            $this->db->exec($qry, $arr);

            $this->redir();
        }
    }

    public function deletePost() {
        if(isset($_GET['reply'])){
            $qry = "DELETE FROM " . $this->cfg->get('reply_tbl') . " WHERE postID=:postID AND userID=:userID";
        }else{
        $qry = "DELETE FROM " . $this->cfg->get('post_tbl') . " WHERE postID=:postID AND userID=:userID";
        }
        $arr = array(
            ':postID' => $_GET['post_id'],
            ':userID' => $userID = $_SESSION['user']['id']
        );
        $this->db->exec($qry, $arr);

        $this->redir();
    }

    public function getReply_To() {
        if (isset($_GET['reply_to'])) {
            echo '&reply_to=' . $_GET['reply_to'];
        }
    }

    private function redir() {
        header('Location: ' . $this->cfg->get('site_url') . $this->cfg->get('index_page'));
    }

}

?>
